/**
* Script to filter special characters and stop words from a tbl file provided 
* by Obofoundry (http://www.berkeleybop.org/ontologies/). Expected structure 
* of the file provided
*
* id	concept name	textual description	synonyms
* 
* @param	file	name of the table file to be filtered 
* @author	Anika Oellrich
*/

/* function to remove all trouble causing special characters and stop words */
public String deleteTrouble(String _s) {
  def s = _s

  s = s.replaceAll(",|\\|\\(|\\)|\\{|\\}|\\[|\\]|\\d|:","")
  s = s.replaceAll("-|(\\/)| and | or | with | i | ii | iiiv | the | of |' | VI | VII | VIII | V | XI ", " ")
  s = s.replaceAll(" th | s ", " ")
  s = s.replaceAll("\\(", "")

  // removing words and special characters leaves double spaces which are
  // to be replaced
  // s = s.unexpand().replace("\t", " ")
  return s
}

// read each line of tbl file, remove unwanted words/signs and print line
new File(args[0]).splitEachLine("\t") { splits ->
  // filtering only for terms possessing textual information
  if ( splits.size() > 1 ) {

    def sb = new StringBuffer(splits[0].trim())
    sb.append("\t")
    sb.append(deleteTrouble(splits[1].trim()))

    // removing all the trouble causing elements from synonyms as well
    if ( splits.size() == 4 ) {
      def synonyms = splits[3].trim().split(";")
      def l = []
      synonyms.each { s -> 
        l.add( deleteTrouble(s) )
      }    

      sb.append("\t")
      sb.append( l.join("|") )
    }

    println sb.toString()
  }
}